package com.neuedu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.common.Result;
import com.neuedu.pojo.Category;
import com.neuedu.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/categories")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分页获取分类信息
     *
     * @param page
     * @param pageSize
     * @param query
     * @return
     */
    @GetMapping("/page")
    public Result<Page<Category>> getCategoryPage(int page, int pageSize, String query) {
        //构造分页构造器
        Page<Category> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getLevel, 0);
        queryWrapper.like(null != query, Category::getName, query);
        queryWrapper.orderByDesc(Category::getSort);
        categoryService.page(pageInfo, queryWrapper);
        List<Category> categories = pageInfo.getRecords().stream().map((item) -> {
            categoryService.getAllChildren(item);
            return item;
        }).collect(Collectors.toList());
        pageInfo.setRecords(categories);
        return Result.success(pageInfo);
    }

    /**
     * 获取分类列表
     *
     * @param level
     * @param status
     * @return
     */
    @GetMapping
    public Result<List<Category>> getCategoryList(Integer level, Boolean status) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        if (level == null) level = 0;
        queryWrapper.eq(Category::getLevel, level);
        queryWrapper.eq(status != null, Category::getStatus, status);
        queryWrapper.orderByDesc(Category::getSort);
        List<Category> categories = categoryService.list(queryWrapper);
        categories.forEach(categoryService::getAllChildren);
        return Result.success(categories);
    }

    /**
     * 添加分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public Result<String> saveCategory(@RequestBody Category category) {
        category.setStatus(true);
        if (category.getLevel() > 2) {
            return Result.error("最多添加三级分类");
        }
        //验证上级分类目录下是否已经存在当前分类名
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, category.getParentId());
        List<Category> categories = categoryService.list(queryWrapper);
        for (Category categoryTmp : categories) {
            if(categoryTmp.getName().equals(category.getName())){
                return Result.error(category.getName()+"分类已存在");
            }
        }

        if (category.getSort() == null) {
            category.setSort(0);
        }
        categoryService.save(category);
        return Result.success("添加分类成功");
    }

    /**
     * 修改分类成功
     *
     * @param category
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody Category category) {
        boolean b = categoryService.updateById(category);
        return Result.success("修改员工信息成功");
    }


    /**
     * 根据id查找分类信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Category> getById(@PathVariable long id) {
        Category category = categoryService.getById(id);
        return Result.success(category);
    }

    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteById(@PathVariable Long id) {
        Category category = categoryService.getById(id);
        categoryService.deleteWithChildren(category);
        return Result.success("删除成功");
    }

}
